﻿using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Bilibili.Common.Log;
using PCLUntils;
using PCLUntils.Json;
using PCLUntils.Plantform;
using System;
using System.Diagnostics;
using System.IO;

namespace Avalonia.BiliBili.Libs.Extensions
{
    public static class Extension
    {
        public static T DeepClone<T>(this T obj)
        {
            try
            {
                var json = obj.ParseJson();
                return json.ParseObject<T>();
            }
            catch { }
            return obj;
        }
        public static Bitmap ToBitmap(this Uri uri)
        {
            try
            {
                switch (uri.Scheme)
                {
                    case "avares":
                        using (var stream = AssetLoader.Open(uri))
                            return new Bitmap(stream);
                }
            }
            catch { }
            return default;
        }
        public static void ExecuteShell(this string shell)
        {
            try
            {
                var process = new Process();
                if (PlantformUntils.System == Platforms.Linux)
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        UseShellExecute = false,
                        FileName = "/bin/bash",
                        CreateNoWindow = true,
                        RedirectStandardOutput = true,
                        Arguments = $"-c \"{shell.Replace("\"", "\\\"")}\""
                    };
                }
                else
                {
                    process.StartInfo = new ProcessStartInfo
                    {
                        UseShellExecute = false,
                        FileName = "cmd.exe",
                        CreateNoWindow = true,
                        RedirectStandardOutput = true,
                        Arguments = $"/c \"{shell.Replace("\"", "\\\"")}\""
                    };
                }
                process.Start();
                string result = process.StandardOutput.ReadToEnd();
                LogManager.Instance.LogInfo("ExecuteShell Result :" + result);
                process.WaitForExit();
                process.Dispose();
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("ExecuteShell", ex);
            }
        }
        public static void InstallUpdate(this FileInfo file)
        {
            try
            {
                Process.Start(file.FullName);
                Process.GetCurrentProcess().Kill();
            }
            catch { }
        }
        public static SolidColorBrush ToBrush(this string color, bool x2 = false)
        {
            var colors = color.ToColor(x2);
            return new SolidColorBrush(colors);
        }
        public static Color ToColor(this string color, bool x2 = false)
        {
            try
            {
                color = color.Replace("#", "");
                if (x2)
                    color = Convert.ToInt32(color).ToString("X2");
                return color.Length switch
                {
                    4 => Color.Parse($"#00{color}"),
                    6 or 8 => Color.Parse($"#{color}"),
                    _ => Colors.Gray,
                };
            }
            catch { }
            return default;
        }
        public static Button CreateIconButton(this string filePath, int width = 32)
        {
            string path = Path.Combine(Bilibili.Common.Extension.GetRoot(false), "Assets", "Icon", filePath);
            var bitmap = new Bitmap(path);
            var image = new Image { Source = bitmap, Width = width };
            var root = new Panel();
            root.Children.Add(image);
            return new Button { Content = root };
        }
        public static T GetItem<T>(this IAvaloniaList<T> list, int index)
        {
            try
            {
                return list[index];
            }
            catch { }
            return default;
        }
        public static void PopupMsg(this Panel control, string msg)
        {
            var popup = control.FindControl<PopupDialog>("popup");
            if (popup == null)
            {
                popup = new PopupDialog(2000) { Name = "popup" };
                control.Children.Add(popup);
            }
            popup.Open(msg);
        }
        public static Bitmap ToBitmap(this string path)
        {
            try
            {
                return new Bitmap(path);
            }
            catch { }
            return null;
        }
    }
}